package com.woniuxy.util;

import javax.xml.crypto.Data;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;

/**
 * @Author: wanghao
 * @Date: 2019/9/18 16:08
 *
 * 处理时间有关的工具类
 */
public class TimeUtil {

    private static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    /**
     * 按照yyyy-MM-dd HH:mm:ss格式输出当前时间
     * @return
     */
    public static String getLocalTime() {
        return dateTimeFormatter.format(LocalDateTime.now());
    }
    /**
     * 按照yyyy-MM-dd HH:mm:ss格式解析字符串时间，返回时间类
     * @return
     */
    public static LocalDateTime parseTime(String time) {
        return LocalDateTime.parse(time, dateTimeFormatter);
    }

    /**
     * 比较两个字符串时间的毫秒之差，返回之差
     * @param startTime
     * @param endTime
     * @return
     */
    public static Long compare(String startTime,String endTime) {
        Duration between = Duration.between(TimeUtil.parseTime(startTime), TimeUtil.parseTime(endTime));
        return between.toMillis();
    }
}
